package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.file.api.SysFileConfigService;
import com.nidu.demo.file.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 文件配置管理
 *
 * @author auto
 */
@Tag(name = "管理后台 - 文件配置")
@RestController
@RequestMapping("/system/file-config")
@Validated
@RequiredArgsConstructor
public class SysFileConfigController {

    private final SysFileConfigService fileConfigService;

    @Operation(summary = "创建文件配置", description = "创建新的文件配置")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:file-config:create')")
    public SingleResponse<FileConfigCO> addFileConfig(@RequestBody FileConfigAddCmd cmd) {
        return fileConfigService.add(cmd);
    }

    @Operation(summary = "更新文件配置", description = "更新文件配置的信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:file-config:update')")
    public SingleResponse<FileConfigCO> updateFileConfig(@PathVariable Long id, @RequestBody FileConfigUpdateCmd cmd) {
        cmd.setId(id);
        return fileConfigService.update(cmd);
    }

    @Operation(summary = "删除文件配置", description = "根据 ID 删除文件配置")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:file-config:delete')")
    public Response deleteFileConfig(@PathVariable Long id) {
        return fileConfigService.delete(id);
    }

    @Operation(summary = "批量删除文件配置", description = "根据 ID 列表删除多个文件配置")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:file-config:delete')")
    public Response deleteFileConfigs(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return fileConfigService.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取文件配置详情", description = "根据 ID 获取文件配置详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:file-config:query')")
    public SingleResponse<FileConfigCO> getFileConfigById(@PathVariable Long id) {
        return fileConfigService.getById(id);
    }

    @Operation(summary = "查询文件配置列表", description = "获取所有文件配置信息")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:file-config:query')")
    public MultiResponse<FileConfigCO> listFileConfigs(FileConfigListQry qry) {
        return fileConfigService.list(qry);
    }

    @Operation(summary = "分页查询文件配置", description = "分页获取文件配置信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:file-config:query')")
    public PageResponse<FileConfigCO> pageFileConfigs(FileConfigPageQry qry) {
        return fileConfigService.page(qry);
    }

    @Operation(summary = "设置主配置", description = "将指定配置设置为主配置")
    @PutMapping("/{id}/master")
    @PreAuthorize("@ss.hasPermission('system:file-config:update')")
    public Response setMasterConfig(@PathVariable Long id) {
        return fileConfigService.updateMaster(id);
    }
}
